import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import Layout2 from '@/layout/admin'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/admin',
    component: Layout2,
    redirect: '/admin/home',
    children: [
      {
        path: 'home',
        name: 'AdminHome',
        component: () => import('@/views/admin/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },

  {
    path: '/admin/problem',
    component: Layout2,
    redirect: '/admin/problem/list',
    meta: { title: '题目管理', icon: 'form' },
    children: [
      {
        path: 'list',
        name: 'AdminProblemList',
        component: () => import('@/views/admin/problem/list/index'),
        meta: { title: '题目列表', icon: 'form' }
      },
      {
        path: 'add',
        name: 'AdminProblemAdd',
        component: () => import('@/views/admin/problem/add/index'),
        meta: { title: '题目维护', icon: 'form' }
      },
      {
        path: 'control',
        name: 'AdminProblemControl',
        component: () => import('@/views/admin/problem/control/index'),
        meta: { title: '导入导出', icon: 'form' }
      },
      {
        path: 'data',
        name: 'AdminProblemData',
        component: () => import('@/views/admin/problem/list/data'),
        meta: { title: '数据编辑', icon: 'form' },
        hidden: true
      }
    ]
  },

  {
    path: '/admin/contest',
    component: Layout2,
    redirect: '/admin/contest/list',
    meta: { title: '竞赛管理', icon: 'form' },
    children: [
      {
        path: 'list',
        name: 'AdminContestList',
        component: () => import('@/views/admin/contest/list/index'),
        meta: { title: '竞赛列表', icon: 'form' }
      },
      {
        path: 'add',
        name: 'AdminContestAdd',
        component: () => import('@/views/admin/contest/add/index'),
        meta: { title: '竞赛维护', icon: 'form' }
      },
      {
        path: 'duplicate',
        name: 'AdminContestDuplicate',
        component: () => import('@/views/admin/contest/duplicate/index'),
        meta: { title: '相似度检测', icon: 'form' }
      },
      {
        path: 'question',
        name: 'AdminContestQuestion',
        component: () => import('@/views/admin/contest/list/question'),
        meta: { title: '消息管理', icon: 'form' },
        hidden: true
      }
    ]
  },

  {
    path: '/admin/record',
    component: Layout2,
    redirect: '/admin/record/list',
    meta: { title: '团队管理', icon: 'form' },
    children: [
      {
        path: 'list',
        name: 'AdminRecordList',
        component: () => import('@/views/admin/record/list/index'),
        meta: { title: '团队列表', icon: 'form' }
      },
      {
        path: 'data',
        name: 'AdminRecordData',
        component: () => import('@/views/admin/record/data/index'),
        meta: { title: '团队数据', icon: 'form' }
      }
    ]
  },

  {
    path: '/admin/user',
    component: Layout2,
    redirect: '/admin/user/user',
    meta: { title: '权限管理', icon: 'form' },
    children: [
      {
        path: 'user',
        name: 'AdminUserUser',
        component: () => import('@/views/admin/user/user/index'),
        meta: { title: '用户管理', icon: 'form' }
      },
      {
        path: 'role',
        name: 'AdminUserRole',
        component: () => import('@/views/admin/user/role/index'),
        meta: { title: '角色管理', icon: 'form' }
      }
    ]
  },

  {
    path: '/admin/setting',
    component: Layout2,
    redirect: '/admin/setting/news',
    meta: { title: '系统管理', icon: 'form' },
    children: [
      {
        path: 'classify',
        name: 'AdminSettingClassify',
        component: () => import('@/views/admin/setting/classify/index'),
        meta: { title: '分类管理', icon: 'form' }
      },
      {
        path: 'news',
        name: 'AdminSettingNews',
        component: () => import('@/views/admin/setting/news/index'),
        meta: { title: '通知管理', icon: 'form' }
      },
      {
        path: 'carousel',
        name: 'AdminSettingCar',
        component: () => import('@/views/admin/setting/carousel/index'),
        meta: { title: '轮播图管理', icon: 'form' }
      }
    ]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/home/index'),
        meta: { title: '主页', icon: 'el-icon-s-home' }
      }
    ]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/problem',
    children: [
      {
        path: 'problem',
        name: 'Problem',
        component: () => import('@/views/problem/index'),
        meta: { title: '题目', icon: 'el-icon-s-order' }
      }
    ]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/status',
    children: [
      {
        path: 'status',
        name: 'Status',
        component: () => import('@/views/status/index'),
        meta: { title: '状态', icon: 'el-icon-s-marketing' }
      }
    ]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/rank',
    // 暂时保留
    hidden: true,
    children: [
      {
        path: 'rank',
        name: 'Rank',
        component: () => import('@/views/rank/index'),
        meta: { title: '排行榜', icon: 'el-icon-s-data' }
      }
    ]
  },

  {
    path: '/contest',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Contest',
        component: () => import('@/views/contest/index'),
        meta: { title: '竞赛', icon: 'el-icon-s-flag' }
      },
      {
        path: 'explain',
        name: 'ContestExplain',
        component: () => import('@/views/contest/explain'),
        meta: { title: '竞赛说明', icon: 'el-icon-s-flag' },
        hidden: true
      },
      {
        path: 'details',
        name: 'ContestDetails',
        component: () => import('@/views/contest/details'),
        meta: { title: '竞赛详情', icon: 'el-icon-s-flag' },
        hidden: true
      }
    ]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/record',
    children: [
      {
        path: 'record',
        name: 'Record',
        component: () => import('@/views/record/index'),
        meta: { title: '记录', icon: 'el-icon-s-help' }
      },
      {
        path: '/record/detail',
        name: 'Detail',
        component: () => import('@/views/record/detail'),
        meta: { title: '记录详情', icon: 'el-icon-s-help' },
        hidden: true
      }
    ]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/help',
    children: [
      {
        path: 'help',
        name: 'Help',
        component: () => import('@/views/help/index'),
        meta: { title: '帮助', icon: 'el-icon-question' }
      }
    ]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/profile',
    hidden: true,
    children: [
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/user/profile'),
        meta: { title: '个人', icon: 'form' }
      }
    ]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/code',
    hidden: true,
    children: [
      {
        path: 'code',
        name: 'Code',
        component: () => import('@/views/problem/code/index'),
        meta: { title: '代码', icon: 'form' }
      }
    ]
  },

  {
    path: '/editor',
    meta: { title: '编辑', icon: 'form' },
    component: () => import('@/views/problem/editor/index'),
    hidden: true
  },

  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/user/login'),
    hidden: true
  },

  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/user/register'),
    hidden: true
  },

  {
    path: '/findPass',
    name: 'FindPass',
    component: () => import('@/views/user/login/findPass'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
